package org.baitiaojun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.reflection.ArrayUtil;
import org.baitiaojun.dao.CheckGroupCheckItemMapper;
import org.baitiaojun.dao.CheckGroupMapper;
import org.baitiaojun.dao.SetMealCheckGroupMapper;
import org.baitiaojun.pojo.CheckGroup;
import org.baitiaojun.pojo.CheckGroupCheckItemKey;
import org.baitiaojun.pojo.CheckItem;
import org.baitiaojun.pojo.SetMealCheckGroupKey;
import org.baitiaojun.service.CheckGroupService;
import org.baitiaojun.service.SetMealCheckGroupService;
import org.baitiaojun.util.MessageConstant;
import org.baitiaojun.vo.PageResult;
import org.baitiaojun.vo.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;

/**
 * @类名 CheckGroupServiceImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/5/25 22:27
 * @版本 1.0
 */

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    @Autowired
    private SetMealCheckGroupMapper setMealCheckGroupMapper;

    @Override
    @Transactional (
            isolation = Isolation.REPEATABLE_READ,
            propagation = Propagation.REQUIRED,
            rollbackFor = RuntimeException.class
    )
    public void saveCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) throws Exception {
        if (checkitemIds.length > 0) {
            CheckGroupCheckItemKey checkGroupCheckItemKey = new CheckGroupCheckItemKey();
            checkGroupCheckItemKey.setCheckgroupId(checkGroup.getId());
            for (Integer checkitemId : checkitemIds) {
                checkGroupCheckItemKey.setCheckitemId(checkitemId);
                int res = checkGroupCheckItemMapper.insertCheckGroupCheckItem(checkGroupCheckItemKey);
                if (res == 0) {
                    throw new Exception(MessageConstant.ADD_CHECKGROUP_CHECKITEM_FAIL);
                }
            }
        }
        int res =  checkGroupMapper.insert(checkGroup);
        if (res == 0) {
            throw new Exception(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @Override
    public PageResult<CheckGroup> queryCheckGroupByPageAndCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String condition = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        List<CheckGroup> checkGroups = checkGroupMapper.selectCheckGroupByPageAndCondition(condition);

        PageResult<CheckGroup> pageResult = new PageResult<>();
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(checkGroups);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRows(checkGroups);

        return pageResult;
    }

    @Override
    public CheckGroup queryCheckGroupList(Integer id) {
        return checkGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional (
            isolation = Isolation.REPEATABLE_READ,
            propagation = Propagation.REQUIRED,
            rollbackFor = RuntimeException.class
    )
    public void editCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) throws Exception {
        Integer id = checkGroup.getId();
        List<Integer> ids = checkGroupCheckItemMapper.selectCheckItemIdsById(id);
        if (ids.size() > 0) {
            int checkGroupCheckRes = checkGroupCheckItemMapper.deleteCheckGroupCheckItemByCheckGroupId(id);
            if (checkGroupCheckRes == 0) {
                throw new Exception(MessageConstant.DELETE_CHECKGROUP_CHECKITEM_FAIL);
            }
        }

        if (checkitemIds.length > 0) {
            CheckGroupCheckItemKey checkGroupCheckItemKey = new CheckGroupCheckItemKey();
            checkGroupCheckItemKey.setCheckgroupId(checkGroup.getId());

            int checkGroupCheckItemRes;
            for (Integer checkitemId:checkitemIds) {
                checkGroupCheckItemKey.setCheckitemId(checkitemId);
                checkGroupCheckItemRes = checkGroupCheckItemMapper.insert(checkGroupCheckItemKey);
                if (checkGroupCheckItemRes == 0) {
                    throw new Exception(MessageConstant.ADD_CHECKGROUP_CHECKITEM_FAIL);
                }
            }
        }

        int checkGroupRes = checkGroupMapper.updateByPrimaryKey(checkGroup);
        if (checkGroupRes == 0) {
            throw new Exception(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @Override
    @Transactional(
            rollbackFor = RuntimeException.class
    )
    public void removeCheckGroup(Integer id) throws Exception {
        List<CheckGroupCheckItemKey> checkGroupCheckItemKeys = checkGroupCheckItemMapper.selectCheckGroupCheckItemByCheckItemId(id);
        if (!ObjectUtils.isEmpty(checkGroupCheckItemKeys)) {
            for (CheckGroupCheckItemKey checkGroupCheckItemKey:checkGroupCheckItemKeys) {
                Integer checkitemId = checkGroupCheckItemKey.getCheckitemId();
                int res = checkGroupCheckItemMapper.deleteCheckGroupCheckItemById(checkitemId);
                if (res == 0) {
                    throw new Exception(MessageConstant.DELETE_CHECKGROUP_CHECKITEM_FAIL);
                }
            }
        }

        List<SetMealCheckGroupKey> setMealCheckGroupKeys = setMealCheckGroupMapper.selectCheckGroupIdsByCheckGroupId(id);
        if (setMealCheckGroupKeys.size() > 0) {
            int setMealCheckGroupRes = setMealCheckGroupMapper.deleteMealCheckGroupByCheckGroupId(id);
            if (setMealCheckGroupRes == 0) {
                throw new Exception(MessageConstant.DELETE_CHECKGROUP_SETMEAL_FAIL);
            }
        }

        int res = checkGroupMapper.deleteByPrimaryKey(id);
        if (res == 0) {
            throw new Exception(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @Override
    public List<CheckGroup> queryAllCheckGroup() {
        return checkGroupMapper.selectAllCheckGroup();
    }
}
